<template>
  <div class="blog-item normal">
    <nuxt-link
      tag="a"
      target="_blank"
      :to="`/detail/${blogItem.id}`"
      class="blog-item-content flex-center"
    >
      <h2 class="blog-item-title">{{blogItem.title}}</h2>
    </nuxt-link>
    <h4 class="author-time ellipsis">
      <p>作者: {{blogItem.user.username}}</p>
      <p>更新时间: {{blogItem.updated_at}}</p>
    </h4>
    <div class="like-word flex-middle" @click.stop>
      <i v-if="blogItem.likes.user_like" class="iconfont icon-liked"></i>
      <i v-else class="iconfont icon-like" @click="handleLikeClick"></i>
      <span>{{blogItem.likes.count}}</span>
    </div>
    <div class="blog-item-cover-img">
      <img v-if="blogItem.cover" :src="blogItem.cover" />
    </div>
  </div>
</template>

<script>
import API from '~/interface/Word';
export default {
  props: {
    blogItem: {
      type: Object,
      default() {
        return {}
      }
    }
  },

  methods: {
    handleLikeClick() {
      const data = {
        type: 'article',
        target_id: this.blogItem.id
      };

      API.postLikeWord(data).then(res => {
        this.blogItem.likes.user_like = true;
        this.blogItem.likes.count += 1;
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.blog-item {
  position: relative;
  height: 150px;
  margin-bottom: 10px;
  background-color: #fff;
  transition: box-shadow 0.5s;
  overflow: hidden;

  .blog-item-content {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    overflow: hidden;
    z-index: 33;
    padding: 10px 15px;
    color: #fff;

    .blog-item-title {
      color: inherit;
      transition: transform 0.5s;
    }
  }

  .author-time {
    position: absolute;
    left: 50%;
    top: 80%;
    transform: translate3d(-50%, -50%, 0);
    transition: top 0.5s, opacity 0.5s;
    opacity: 0;
    z-index: 3;
    color: #fff;

    p {
      text-align: center;
      margin: 0;
      height: 18px;
    }
  }

  .blog-item-cover-img {
    width: 100%;
    height: 100%;
    background-color: rgba($color: #000, $alpha: 0.38);

    &::before,
    &::after {
      content: '';
      position: absolute;
      width: 0;
      height: 0;
      transition: width 0.5s, height 0.5s;
      z-index: 1;
    }

    &::before {
      left: 0;
      top: 0;
      border: solid var(--theme);
      border-width: 2px 0 0 2px;
    }

    &::after {
      right: 0;
      bottom: 0;
      border: solid var(--theme);
      border-width: 0 2px 2px 0;
    }

    img {
      filter: blur(8px);
      transform: scale(1.1);
      transition: filter 0.5s;
    }
  }

  &:hover {
    box-shadow: 3px 3px 8px #ababab;

    .blog-item-title {
      transform: translateY(-80%);
    }

    .author-time {
      top: 55%;
      opacity: 1;
    }

    .blog-item-cover-img::before,
    .blog-item-cover-img::after {
      width: 100%;
      height: 100%;
    }
  }

  .like-word {
    position: absolute;
    left: 0;
    bottom: 0;
    padding: 5px 10px;
    z-index: 99;
    color: #fff;

    .iconfont {
      font-size: 18px;
      cursor: pointer;
      margin-right: 5px;
    }

    .icon-liked {
      color: #ff5656;
    }
  }
}
</style>